package com.xm.xmapp;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.xm.xmapp.app.CodeConstant;
import com.xm.xmapp.app.MyAppcation;
import com.xm.xmapp.app.Type;
import com.xm.xmapp.base.BaseActivity;
import com.xm.xmapp.myinterface.DisssmissInterFace;
import com.xm.xmapp.myinterface.GetDataInterFace;
import com.xm.xmapp.request.bean.RequestUploadDriverLicense;
import com.xm.xmapp.response.bean.ResponseDriverBean;
import com.xm.xmapp.response.bean.ResponseGetDriverLicenseReviewInfo;
import com.xm.xmapp.tools.ImageTool;
import com.xm.xmapp.tools.ScreenUtils;
import com.xm.xmapp.tools.Tools;

public class AuthorizeDriverActivity extends BaseActivity implements OnClickListener {

//	private ResponseDriverBean.Card mDriverBean;
	private TextView sumbit;
	private TextView tv_no_pass_message;
	private LinearLayout ll_error_lay;
	private ResponseGetDriverLicenseReviewInfo bean;
	private ImageView iv_driving_license_fort, iv_driving_license_back;
	private RequestUploadDriverLicense requestBean;

	@Override
	public Object dataChang(int flag, Object data) {
		switch (flag) {
		case Type.A_Authorize_Driver.GET_BITMAP:
			Bitmap bitmap = (Bitmap) data;
			iv_driving_license_fort.setImageBitmap(bitmap);
			requestBean.driving_license_front_img = "suffix:jpg;base64;" + ImageTool.Bitmap2StrByBase64(bitmap);
			break;
		case Type.A_Authorize_Driver.GET_DATA:
//			mDriverBean = (ResponseDriverBean.Card) data;
			break;

		default:
			break;
		}
		return super.dataChang(flag, data);
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_authorize_driver);
	}

	@Override
	public void initView() {
		ll_error_lay = (LinearLayout) findViewById(R.id.ll_error_lay);
		sumbit = (TextView) findViewById(R.id.sumbit);
		tv_no_pass_message = (TextView) findViewById(R.id.tv_no_pass_message);
		iv_driving_license_fort = (ImageView) findViewById(R.id.iv_driving_license_fort);
		iv_driving_license_back = (ImageView) findViewById(R.id.iv_driving_license_back);
	}

	@Override
	public void initData() {
		setMyTitle("驾照审核");
		requestBean = new RequestUploadDriverLicense();
		bean = (ResponseGetDriverLicenseReviewInfo) getIntent().getSerializableExtra("bean");
		if (bean ==null || bean.remark == null || bean.remark.equals("")) {
			ll_error_lay.setVisibility(View.GONE);
		} else {
			ll_error_lay.setVisibility(View.VISIBLE);
			tv_no_pass_message.setText(bean.remark);
		}
	}

	@Override
	public void initEvent() {
		iv_driving_license_fort.setOnClickListener(this);
		iv_driving_license_back.setOnClickListener(this);
		sumbit.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.iv_driving_license_fort:
			intent = new Intent(getApplicationContext(), TakePhotoActivity.class);
			intent.putExtra("isIdCard", false);
			startActivityForResult(intent, CodeConstant.TakePhoto); // 用户点击了从相机获取
			break;
		case R.id.iv_driving_license_back:
			intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); // 调用系统相机
			Uri imageUri = Uri.fromFile(new File(CodeConstant.PhotoPath));// 指定照片保存路径（SD卡），image.jpg为一个临时文件，每次拍照后这个图片都会被替换
			intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
			startActivityForResult(intent, CodeConstant.TakePhoto); // 用户点击了从相机获取
			break;
		case R.id.sumbit:
			if (requestBean.driving_license_front_img == null || requestBean.driving_license_copy_img == null) {
				Tools.showMessage("请先添加完照片");
			} else {
				upload_user_audit_imgRequest();
			}
			break;

		default:
			break;
		}

	}

	/**
	 * 设置驾照过期日期
	 */
//	private void setDriving_license_expire() {
//		if (mDriverBean != null) {
//
//			if (mDriverBean.valid_date != null) {
//				String[] str = mDriverBean.valid_date.split("至");
//				if (str.length > 1) {
//					requestBean.expire_date = str[1];
//				} else {
//					return;
//				}
//			} else {
//				// valid_from valid_for
//				try {
//
//					SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); // 日期格式
//					Date startDate = dateFormat.parse(mDriverBean.valid_from); // 指定日期
//					Calendar calendar = Calendar.getInstance();
//					calendar.setTime(startDate);
//					String strYears = mDriverBean.valid_for;
//					strYears = strYears.replaceAll("年", "");
//					int years = Integer.parseInt(strYears);
//					calendar.add(Calendar.YEAR, years);
//					requestBean.expire_date = dateFormat.format(calendar.getTime());
//
//				} catch (Exception e) {
//					Tools.showMessage("异常错误");
//					return;
//				}
//			}
//
//		}
//	}
	
	private void upload_user_audit_imgRequest() {
		promptDialog.show();
//		setDriving_license_expire();
		MyAppcation.getMyAppcation().getPostData(this, requestBean, new GetDataInterFace<String>() {
			
			@Override
			public void onResponse(String response) {
				promptDialog.dismiss();
				Tools.showMessage("上传成功");
				finish();
			}
		}, new DisssmissInterFace() {
			
			@Override
			public void putError(VolleyError error) {
				promptDialog.dismiss();
			}
		});
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (resultCode == RESULT_OK) {
			switch (requestCode) {
			case CodeConstant.TakePhoto:
				String status = Environment.getExternalStorageState();
				if (status.equals(Environment.MEDIA_MOUNTED)) { // 是否有SD卡
					Bitmap bitmap = null;
					int width = ScreenUtils.getScreenWidth(this);
					int height = ScreenUtils.getScreenHeight(this);
					if (width > 720) {
						bitmap = ImageTool.getSmallBitmap(CodeConstant.PhotoPath, 720, 1280);
					} else {
						bitmap = ImageTool.getSmallBitmap(CodeConstant.PhotoPath, width, height);
					}
					iv_driving_license_back.setImageBitmap(bitmap);
					requestBean.driving_license_copy_img = "suffix:jpg;base64;" + ImageTool.Bitmap2StrByBase64(bitmap);

				} else {
					Tools.showMessage("没有SD卡");
				}
				break;

			default:
				break;
			}
		}
	}
}
